package com.fhd.service.Client;

import com.fhd.dao.BaseDao;
import com.fhd.dao.Client.ClientDao;
import com.fhd.dao.Client.ClientDaoImplement;
import com.fhd.pojo.Client;
import java.sql.Connection;
import java.sql.Date;
import java.util.Objects;

public class ClientServiceImplement implements ClientService{
    // 业务层调用Dao层
    private ClientDao clientDao = null;
    public ClientServiceImplement() {
        clientDao = new ClientDaoImplement();
    }

    // 客户登录
    @Override
    public Client login(String userCode, String userPassword) {
        Connection conn = null;
        Client client = null;
        try {
            // 获取连接，
            conn = BaseDao.getConnection();
            client = clientDao.getLoginClient(conn, userCode);

            //判断找到的客户不为空的前提下密码是否对
            if (client != null) {
                if (!Objects.equals(client.getUserPassword(),userPassword)) {
                    client = null;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return client;
    }


    // 客户注册
    @Override
    public int signup(String userCode, String userPassword, String userName) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = clientDao.signUpClient(conn,userCode,userPassword,userName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 修改个人信息
    @Override
    public int updateInformation(int id, String userCode, String userPassword, String userName) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = clientDao.updateInformation(conn,id,userCode,userPassword,userName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 更改上次签到日期
    @Override
    public int updateLastPointsDate(int id, int points, Date newDate) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = clientDao.updateLastPointsDate(conn,id,points,newDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 更改余额
    @Override
    public int updateMoney(int id, int money) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = clientDao.updateMoney(conn,id,money);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 重新获取金额
    @Override
    public int getMoney(int id) {
        Connection conn = null;
        int money = -1;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            money = clientDao.getMoney(conn,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return money;
    }
}
